﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CNotebook;
using CNotebook.Inventory;

namespace NotebookS2
{
    public partial class ManufacturingOrder : Form
    {
        public ManufacturingOrder()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }
        public string ActiveUserId { get; set; }

        void BindWarehouse()
        {
            WarehouseManager oManager = new WarehouseManager();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable oTable = new DataTable();

            oTable = oManager.GetWarehouse();



            cboReceivingWarehouse.Visible = false;
            cboReceivingWarehouse.Properties.DataSource = oTable;
            cboReceivingWarehouse.Properties.DisplayMember = "Dscription";
            cboReceivingWarehouse.Properties.ValueMember = "Code";
            DevExpress.XtraEditors.Controls.LookUpColumnInfo col;

            col = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Dscription", "Dscription", 100);

            col.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;

            cboReceivingWarehouse.Properties.Columns.Add(col);
            cboReceivingWarehouse.Properties.ShowHeader = false;
            cboReceivingWarehouse.Visible = true;
            oManager.Close();
        }


        void BindProductStructure(string _ItemCode)
        {
            BOMManager oManager = new BOMManager();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable oTable = new DataTable();

            oTable = oManager.GetProductStructure(_ItemCode);



            cboProductStructure.Visible = false;
            cboProductStructure.Properties.DataSource = oTable;
            cboProductStructure.Properties.DisplayMember = "BOMName";
            cboProductStructure.Properties.ValueMember = "BOMCode";
            DevExpress.XtraEditors.Controls.LookUpColumnInfo col;

            col = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("BOMName", "BOMName", 100);

            col.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;

            cboProductStructure.Properties.Columns.Add(col);
            cboProductStructure.Properties.ShowHeader = false;
            cboProductStructure.Visible = true;
            oManager.Close();
        }

        private void ManufacturingOrder_Load(object sender, EventArgs e)
        {

            SqlConnection oConnection = new SqlConnection();
            oConnection.ConnectionString = this.ConnectionString;
            oConnection.Open();

            SqlCommand oCommand = new SqlCommand("SELECT * FROM ORDR", oConnection);
            oCommand.Connection = oConnection;

            SqlDataAdapter oAdapter = new SqlDataAdapter();
            DataTable dt = new DataTable();

            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(dt);

            schedulerStorage1.Resources.DataSource = dt;


            cboProductionType.SelectedIndex = 0;
            txtCreatedBy.Text = this.ActiveUserId;
            dtDateCreated.EditValue = System.DateTime.Now;
            dtDateModified.EditValue = System.DateTime.Now;


            dtDateNeeded.EditValue = System.DateTime.Now;

            BindWarehouse();
        }

        private void txtProductCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FND_ITM_PRD_ONLY oFindItemForm = new FND_ITM_PRD_ONLY();
            
            oFindItemForm.ConnectionString = this.ConnectionString;
            
            oFindItemForm.ShowDialog();

            string sItemCode = oFindItemForm.SelectedItemCode;

            if (sItemCode != null)
            {
                ItemManager oManager = new ItemManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();
                DataTable oTable = oManager.GetItemMasterData(sItemCode);
                oManager.Close();



                txtProductCode.Text = oTable.Rows[0]["ItemCode"].ToString();
                txtProductDscription.Text = oTable.Rows[0]["Description"].ToString();


                BindProductStructure(txtProductCode.Text);

            }
        }

        private void cboProductStructure_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                BOMManager oManager = new BOMManager();

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();
                DataTable dt = oManager.GetMaterials(cboProductStructure.EditValue.ToString());
                oManager.Close();

                gridControl1.DataSource = dt;
            }
            catch { }
            gridControl1.Refresh();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ManufacturingOrderManager oManager = new ManufacturingOrderManager();
            ManufacturingOrderUnit oUnit = new ManufacturingOrderUnit();


            oManager.ConnectionString = this.ConnectionString;

            oManager.Open();



            oManager.Close();
            
        }
    }
}
